<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Socket.IO | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C09-IM/Chat.html" />
    
    
    <link rel="prev" href="../C09-IM/WebSocket.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="9.3"
        data-chapter-title="Socket.IO"
        data-filepath="C09-IM/SocketIO.md"
        data-basepath=".."
        data-revision="Mon May 27 2019 03:35:45 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Introduction/index.html">
            
                
                    <a href="../C01-Introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Introduction/AboutToutiaoWebVM.html">
            
                
                    <a href="../C01-Introduction/AboutToutiaoWebVM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        ToutiaoWeb虚拟机使用说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Introduction/PycharmRemoteDevelop.html">
            
                
                    <a href="../C01-Introduction/PycharmRemoteDevelop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Pycharm远程开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Introduction/AboutProject.html">
            
                
                    <a href="../C01-Introduction/AboutProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        产品与开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Database/index.html">
            
                
                    <a href="../C02-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Database/DatabaseDesign.html">
            
                
                    <a href="../C02-Database/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Database/ORM.html">
            
                
                    <a href="../C02-Database/ORM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        理解ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Database/SQLAlchemyMapping.html">
            
                
                    <a href="../C02-Database/SQLAlchemyMapping.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        SQLAlchemy映射构建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Database/SQLAlchemyOperations.html">
            
                
                    <a href="../C02-Database/SQLAlchemyOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        SQLAlchemy操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Database/DatabaseTheory.html">
            
                
                    <a href="../C02-Database/DatabaseTheory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        数据库理论
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Database/DistributedID.html">
            
                
                    <a href="../C02-Database/DistributedID.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        分布式ID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Database/DatabaseOptimize.html">
            
                
                    <a href="../C02-Database/DatabaseOptimize.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        数据库优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Database/Redis.html">
            
                
                    <a href="../C02-Database/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Gitflow/index.html">
            
                
                    <a href="../C03-Gitflow/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Git工作流
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Gitflow/Gitflow.html">
            
                
                    <a href="../C03-Gitflow/Gitflow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Gitflow
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Gitflow/AboutGit.html">
            
                
                    <a href="../C03-Gitflow/AboutGit.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Git总结
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Gitflow/ProjectDirectory.html">
            
                
                    <a href="../C03-Gitflow/ProjectDirectory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        工程目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Gitflow/DebugMethod.html">
            
                
                    <a href="../C03-Gitflow/DebugMethod.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        调试方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-JWT/index.html">
            
                
                    <a href="../C04-JWT/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-JWT/JWT.html">
            
                
                    <a href="../C04-JWT/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        JWT &amp; JWS &amp; JWE
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-JWT/PythonLib.html">
            
                
                    <a href="../C04-JWT/PythonLib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        PythonLib
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-JWT/Implement.html">
            
                
                    <a href="../C04-JWT/Implement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        实施方案
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-JWT/Forbbiden.html">
            
                
                    <a href="../C04-JWT/Forbbiden.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        禁用问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-OSS/index.html">
            
                
                    <a href="../C05-OSS/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象存储
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-OSS/Qiniu.html">
            
                
                    <a href="../C05-OSS/Qiniu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        七牛云存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-OSS/CDN.html">
            
                
                    <a href="../C05-OSS/CDN.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        CDN
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Cache/index.html">
            
                
                    <a href="../C06-Cache/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        缓存
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Cache/CacheArchitecture.html">
            
                
                    <a href="../C06-Cache/CacheArchitecture.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        缓存的架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Cache/CacheData.html">
            
                
                    <a href="../C06-Cache/CacheData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Cache/CacheTTLAndEviction.html">
            
                
                    <a href="../C06-Cache/CacheTTLAndEviction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        缓存的有效期与淘汰策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C06-Cache/CachePatterns.html">
            
                
                    <a href="../C06-Cache/CachePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        缓存模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C06-Cache/CacheProblems.html">
            
                
                    <a href="../C06-Cache/CacheProblems.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        缓存问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C06-Cache/ProjectCacheAndStorageDesign.html">
            
                
                    <a href="../C06-Cache/ProjectCacheAndStorageDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        项目缓存与Redis持久存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="C06-Cache/ProjectCacheImplement.html">
            
                
                    <a href="../C06-Cache/ProjectCacheImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        项目缓存实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="C06-Cache/ProjectRedisStorageImplement.html">
            
                
                    <a href="../C06-Cache/ProjectRedisStorageImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        项目Redis持久存储实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-APScheduler/index.html">
            
                
                    <a href="../C07-APScheduler/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        APScheduler
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C07-APScheduler/APSchedulerTutorial.html">
            
                
                    <a href="../C07-APScheduler/APSchedulerTutorial.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        APScheduler使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C07-APScheduler/FixStatistics.html">
            
                
                    <a href="../C07-APScheduler/FixStatistics.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定时修正统计数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-RPC/index.html">
            
                
                    <a href="../C08-RPC/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        RPC
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C08-RPC/AboutRPC.html">
            
                
                    <a href="../C08-RPC/AboutRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        RPC简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C08-RPC/gRPC.html">
            
                
                    <a href="../C08-RPC/gRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        gRPC
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C08-RPC/ProtocolBuffers.html">
            
                
                    <a href="../C08-RPC/ProtocolBuffers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        Protocol Buffers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C08-RPC/RecoSysIDL.html">
            
                
                    <a href="../C08-RPC/RecoSysIDL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        推荐系统接口定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C08-RPC/Server.html">
            
                
                    <a href="../C08-RPC/Server.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="C08-RPC/Client.html">
            
                
                    <a href="../C08-RPC/Client.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="C08-RPC/ToutiaoAPIViewImplement.html">
            
                
                    <a href="../C08-RPC/ToutiaoAPIViewImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        头条首页新闻推荐接口编写
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-IM/index.html">
            
                
                    <a href="../C09-IM/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        即时通信
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-IM/AboutIM.html">
            
                
                    <a href="../C09-IM/AboutIM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        即时通信简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C09-IM/WebSocket.html">
            
                
                    <a href="../C09-IM/WebSocket.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="9.3" data-path="C09-IM/SocketIO.html">
            
                
                    <a href="../C09-IM/SocketIO.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        Socket.IO
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C09-IM/Chat.html">
            
                
                    <a href="../C09-IM/Chat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        头条聊天服务实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C09-IM/MessagePush.html">
            
                
                    <a href="../C09-IM/MessagePush.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        头条在线消息推送实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Elasticsearch/index.html">
            
                
                    <a href="../C10-Elasticsearch/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Elasticsearch
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="C10-Elasticsearch/Introduction.html">
            
                
                    <a href="../C10-Elasticsearch/Introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        简介与原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="C10-Elasticsearch/ConceptAndCluster.html">
            
                
                    <a href="../C10-Elasticsearch/ConceptAndCluster.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        概念与集群
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="C10-Elasticsearch/AnalysisIK.html">
            
                
                    <a href="../C10-Elasticsearch/AnalysisIK.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        IK中文分析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="C10-Elasticsearch/IndexAndType.html">
            
                
                    <a href="../C10-Elasticsearch/IndexAndType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        索引与类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="C10-Elasticsearch/Document.html">
            
                
                    <a href="../C10-Elasticsearch/Document.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="C10-Elasticsearch/Logstash.html">
            
                
                    <a href="../C10-Elasticsearch/Logstash.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        Logstash导入数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="C10-Elasticsearch/Query.html">
            
                
                    <a href="../C10-Elasticsearch/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="C10-Elasticsearch/ToutiaoSearchImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSearchImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        头条全文检索实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="C10-Elasticsearch/Suggest.html">
            
                
                    <a href="../C10-Elasticsearch/Suggest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        suggest查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="C10-Elasticsearch/ToutiaoSuggestImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSuggestImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        头条suggest查询实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="C11-Unittest.html">
            
                
                    <a href="../C11-Unittest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="C12-Deployment/index.html">
            
                
                    <a href="../C12-Deployment/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        部署相关
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="C12-Deployment/Gunicorn.html">
            
                
                    <a href="../C12-Deployment/Gunicorn.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        Gunicorn
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="C12-Deployment/Supervisor.html">
            
                
                    <a href="../C12-Deployment/Supervisor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        Supervisor
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="socketio">Socket.IO</h1>
<h2 id="1-&#x7B80;&#x4ECB;">1 &#x7B80;&#x4ECB;</h2>
<p> <strong>Socket.IO &#x672C;&#x662F;&#x4E00;&#x4E2A;&#x9762;&#x5411;&#x5B9E;&#x65F6; web &#x5E94;&#x7528;&#x7684; JavaScript &#x5E93;&#xFF0C;&#x73B0;&#x5728;&#x5DF2;&#x6210;&#x4E3A;&#x62E5;&#x6709;&#x4F17;&#x591A;&#x8BED;&#x8A00;&#x652F;&#x6301;&#x7684;Web&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x5E94;&#x7528;&#x7684;&#x6846;&#x67B6;&#x3002;</strong></p>
<p>Socket.IO &#x4E3B;&#x8981;&#x4F7F;&#x7528;WebSocket&#x534F;&#x8BAE;&#x3002;&#x4F46;&#x662F;&#x5982;&#x679C;&#x9700;&#x8981;&#x7684;&#x8BDD;&#xFF0C;Socket.io&#x53EF;&#x4EE5;&#x56DE;&#x9000;&#x5230;&#x51E0;&#x79CD;&#x5176;&#x5B83;&#x65B9;&#x6CD5;&#xFF0C;&#x4F8B;&#x5982;Adobe Flash Sockets&#xFF0C;JSONP&#x62C9;&#x53D6;&#xFF0C;&#x6216;&#x662F;&#x4F20;&#x7EDF;&#x7684;AJAX&#x62C9;&#x53D6;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x540C;&#x65F6;&#x63D0;&#x4F9B;&#x5B8C;&#x5168;&#x76F8;&#x540C;&#x7684;&#x63A5;&#x53E3;&#x3002;&#x5C3D;&#x7BA1;&#x5B83;&#x53EF;&#x4EE5;&#x88AB;&#x7528;&#x4F5C;WebSocket&#x7684;&#x5305;&#x88C5;&#x5E93;&#xFF0C;&#x5B83;&#x8FD8;&#x662F;&#x63D0;&#x4F9B;&#x4E86;&#x8BB8;&#x591A;&#x5176;&#x5B83;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;&#x5E7F;&#x64AD;&#x81F3;&#x591A;&#x4E2A;&#x5957;&#x63A5;&#x5B57;&#xFF0C;&#x5B58;&#x50A8;&#x4E0E;&#x4E0D;&#x540C;&#x5BA2;&#x6237;&#x6709;&#x5173;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x548C;&#x5F02;&#x6B65;IO&#x64CD;&#x4F5C;&#x3002;</p>
<p><strong>Socket.IO  &#x4E0D;&#x7B49;&#x4EF7;&#x4E8E; WebSocket</strong>&#xFF0C;WebSocket&#x53EA;&#x662F;Socket.IO&#x5B9E;&#x73B0;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x7684;&#x5176;&#x4E2D;&#x4E00;&#x79CD;&#x6280;&#x672F;&#x4F9D;&#x8D56;&#xFF0C;&#x800C;&#x4E14;Socket.IO&#x8FD8;&#x5728;&#x5B9E;&#x73B0;WebSocket&#x534F;&#x8BAE;&#x65F6;&#x505A;&#x4E86;&#x4E00;&#x4E9B;&#x8C03;&#x6574;&#x3002;</p>
<h5 id="&#x4F18;&#x70B9;&#xFF1A;">&#x4F18;&#x70B9;&#xFF1A;</h5>
<p>Socket.IO &#x4F1A;&#x81EA;&#x52A8;&#x9009;&#x62E9;&#x5408;&#x9002;&#x53CC;&#x5411;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#xFF0C;&#x4EC5;&#x4EC5;&#x9700;&#x8981;&#x7A0B;&#x5E8F;&#x5458;&#x5BF9;&#x5957;&#x63A5;&#x5B57;&#x7684;&#x6982;&#x5FF5;&#x6709;&#x6240;&#x4E86;&#x89E3;&#x3002;</p>
<p>&#x6709;Python&#x5E93;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;Python&#x5B9E;&#x73B0;&#x7684;Web&#x5E94;&#x7528;&#x4E2D;&#x53BB;&#x5B9E;&#x73B0;IM&#x540E;&#x53F0;&#x670D;&#x52A1;&#x3002;</p>
<h5 id="&#x7F3A;&#x70B9;&#xFF1A;">&#x7F3A;&#x70B9;&#xFF1A;</h5>
<p>Socket.io&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x672C;&#x7684;&#x3001;&#x72EC;&#x7ACB;&#x7684;&#x3001;&#x80FD;&#x591F;&#x56DE;&#x9000;&#x5230;&#x5176;&#x5B83;&#x5B9E;&#x65F6;&#x534F;&#x8BAE;&#x7684;WebSocket&#x5E93;&#xFF0C;&#x5B83;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x4F9D;&#x8D56;&#x4E8E;&#x5176;&#x5B83;&#x5B9E;&#x65F6;&#x4F20;&#x8F93;&#x534F;&#x8BAE;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x5B9E;&#x65F6;&#x4F20;&#x8F93;&#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#x3002;&#x8BE5;&#x534F;&#x8BAE;&#x7684;&#x534F;&#x5546;&#x90E8;&#x5206;&#x4F7F;&#x5F97;&#x652F;&#x6301;&#x6807;&#x51C6;WebSocket&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x8FDE;&#x63A5;&#x5230;Socket.io&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5E76;&#x4E14;&#x652F;&#x6301;Socket.io&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x4E5F;&#x4E0D;&#x80FD;&#x4E0E;&#x975E;Socket.io&#x6846;&#x67B6;&#x7684;WebSocket&#x6216;Comet&#x670D;&#x52A1;&#x5668;&#x901A;&#x4FE1;&#x3002;&#x56E0;&#x800C;&#xFF0C;Socket.io&#x8981;&#x6C42;&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5747;&#x987B;&#x4F7F;&#x7528;&#x8BE5;&#x6846;&#x67B6;&#x3002;</p>
<h2 id="2-python&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5F00;&#x53D1;">2 Python&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5F00;&#x53D1;</h2>
<p>&#x6587;&#x6863;<a href="https://python-socketio.readthedocs.io/en/latest/server.html" target="_blank">https://python-socketio.readthedocs.io/en/latest/server.html</a></p>
<h3 id="&#x5B89;&#x88C5;">&#x5B89;&#x88C5;</h3>
<pre><code class="lang-python">pip install python-socketio
</code></pre>
<h3 id="&#x521B;&#x5EFA;&#x670D;&#x52A1;&#x5668;">&#x521B;&#x5EFA;&#x670D;&#x52A1;&#x5668;</h3>
<ul>
<li><p>&#x65B9;&#x5F0F;1</p>
<p>  &#x4F7F;&#x7528;&#x591A;&#x8FDB;&#x7A0B;&#x591A;&#x7EBF;&#x7A0B;&#x6A21;&#x5F0F;&#x7684;WSGI&#x670D;&#x52A1;&#x5668;&#x5BF9;&#x63A5;&#xFF08;&#x5982;uWSGI&#x3001;gunicorn)</p>
<pre><code class="lang-python">  <span class="hljs-keyword">import</span> socketio  

  <span class="hljs-comment"># create a Socket.IO servers</span>
  sio = socketio.Server()

  <span class="hljs-comment"># &#x6253;&#x5305;&#x6210;WSGI&#x5E94;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;WSGI&#x670D;&#x52A1;&#x5668;&#x6258;&#x7BA1;&#x8FD0;&#x884C;</span>
  app = socketio.WSGIApp(sio)  <span class="hljs-comment"># Flask  Django</span>
</code></pre>
<p>  &#x521B;&#x5EFA;&#x597D;app&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x4F7F;&#x7528;uWSGI&#x3001;&#x6216;gunicorn&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;&#x6B64;&#x5BF9;&#x8C61;&#x3002;</p>
</li>
<li><p>&#x65B9;&#x5F0F;2</p>
<p>  &#x4F5C;&#x4E3A;Flask&#x3001;Django &#x5E94;&#x7528;&#x4E2D;&#x7684;&#x4E00;&#x90E8;&#x5206;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">from</span> wsgi <span class="hljs-keyword">import</span> app  <span class="hljs-comment"># a Flask, Django, etc. application</span>
  <span class="hljs-keyword">import</span> socketio

  <span class="hljs-comment"># create a Socket.IO server</span>
  sio = socketio.Server()

  app = socketio.WSGIApp(sio, app)
</code></pre>
<p>  &#x521B;&#x5EFA;&#x597D;app&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x4F7F;&#x7528;uWSGI&#x3001;&#x6216;gunicorn&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;&#x6B64;&#x5BF9;&#x8C61;&#x3002;</p>
</li>
<li><p>&#x65B9;&#x5F0F;3 </p>
<p>  &#x4F7F;&#x7528;&#x534F;&#x7A0B;&#x7684;&#x65B9;&#x5F0F;&#x8FD0;&#x884C; (&#x63A8;&#x8350;)</p>
<pre><code class="lang-python">  <span class="hljs-keyword">import</span> eventlet
  eventlet.monkey_patch()

  <span class="hljs-keyword">import</span> socketio
  <span class="hljs-keyword">import</span> eventlet.wsgi

  sio = socketio.Server(async_mode=<span class="hljs-string">&apos;eventlet&apos;</span>)  <span class="hljs-comment"># &#x6307;&#x660E;&#x5728;evenlet&#x6A21;&#x5F0F;&#x4E0B;</span>
  app = socketio.Middleware(sio)
  eventlet.wsgi.server(eventlet.listen((<span class="hljs-string">&apos;&apos;</span>, <span class="hljs-number">8000</span>)), app)
</code></pre>
</li>
</ul>
<h5 id="&#x8BF4;&#x660E;">&#x8BF4;&#x660E;</h5>
<p>&#x56E0;&#x4E3A;&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x8FDB;&#x884C;&#x5373;&#x65F6;&#x901A;&#x4FE1;&#x65F6;&#xFF0C;&#x4F1A;&#x5C3D;&#x53EF;&#x80FD;&#x7684;&#x4F7F;&#x7528;&#x957F;&#x8FDE;&#x63A5;&#xFF0C;&#x6240;&#x4EE5;&#x82E5;&#x670D;&#x52A1;&#x5668;&#x91C7;&#x7528;&#x591A;&#x8FDB;&#x7A0B;&#x6216;&#x591A;&#x7EBF;&#x7A0B;&#x65B9;&#x5F0F;&#x8FD0;&#x884C;&#xFF0C;&#x53D7;&#x9650;&#x4E8E;&#x670D;&#x52A1;&#x5668;&#x80FD;&#x521B;&#x5EFA;&#x7684;&#x8FDB;&#x7A0B;&#x6216;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x80FD;&#x591F;&#x652F;&#x6301;&#x7684;&#x5E76;&#x53D1;&#x8FDE;&#x63A5;&#x5BA2;&#x6237;&#x7AEF;&#x4E0D;&#x4F1A;&#x5F88;&#x9AD8;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x670D;&#x52A1;&#x5668;&#x6027;&#x80FD;&#x6709;&#x9650;&#x3002;&#x91C7;&#x7528;&#x534F;&#x7A0B;&#x65B9;&#x5F0F;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x63D0;&#x5347;&#x5373;&#x65F6;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x5668;&#x7684;&#x6027;&#x80FD;&#x3002;</p>
<h3 id="&#x4E8B;&#x4EF6;&#x5904;&#x7406;">&#x4E8B;&#x4EF6;&#x5904;&#x7406;</h3>
<p>&#x4E0D;&#x540C;&#x4E8E;HTTP&#x670D;&#x52A1;&#x7684;&#x7F16;&#x5199;&#x65B9;&#x5F0F;&#xFF0C;SocketIO&#x670D;&#x52A1;&#x7F16;&#x5199;&#x4E0D;&#x518D;&#x4EE5;&#x8BF7;&#x6C42;Request&#x548C;&#x54CD;&#x5E94;Response&#x6765;&#x5904;&#x7406;&#xFF0C;&#x800C;&#x662F;&#x5BF9;&#x6536;&#x53D1;&#x7684;&#x6570;&#x636E;&#x4EE5;<strong>&#x6D88;&#x606F;&#xFF08;message&#xFF09;</strong>&#x6765;&#x5BF9;&#x5F85;&#xFF0C;&#x6536;&#x53D1;&#x7684;&#x4E0D;&#x540C;&#x7C7B;&#x522B;&#x7684;&#x6D88;&#x606F;&#x6570;&#x636E;&#x53C8;&#x4EE5;<strong>&#x4E8B;&#x4EF6;&#xFF08;event&#xFF09;</strong>&#x6765;&#x533A;&#x5206;&#x3002;</p>
<p>&#x539F;&#x672C;HTTP&#x670D;&#x52A1;&#x7F16;&#x5199;&#x4E2D;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x3001;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x7684;&#x89C6;&#x56FE;&#x5904;&#x7406;&#x51FD;&#x6570;&#x5728;SocketIO&#x670D;&#x52A1;&#x4E2D;&#x6539;&#x4E3A;&#x7F16;&#x5199;&#x6536;&#x53D1;&#x4E0D;&#x540C;&#x4E8B;&#x4EF6;&#x7684;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x51FD;&#x6570;&#x3002;</p>
<h4 id="1&#xFF09;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x65B9;&#x6CD5;">1&#xFF09;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x65B9;&#x6CD5;</h4>
<p>&#x7F16;&#x5199;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x6307;&#x5B9A;&#x7684;&#x4E8B;&#x4EF6;&#x6D88;&#x606F;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x5728;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x4E2D;&#x5BF9;&#x6D88;&#x606F;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@sio.on(&apos;connect&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_connect</span><span class="hljs-params">(sid, environ)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x5EFA;&#x7ACB;&#x597D;&#x8FDE;&#x63A5;&#x540E;&#x88AB;&#x6267;&#x884C;
    :param sid: string sid&#x662F;socketio&#x4E3A;&#x5F53;&#x524D;&#x8FDE;&#x63A5;&#x5BA2;&#x6237;&#x7AEF;&#x751F;&#x6210;&#x7684;&#x8BC6;&#x522B;id
    :param environ: dict &#x5728;&#x8FDE;&#x63A5;&#x63E1;&#x624B;&#x65F6;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x63E1;&#x624B;&#x6570;&#x636E;(HTTP&#x62A5;&#x6587;&#x89E3;&#x6790;&#x4E4B;&#x540E;&#x7684;&#x5B57;&#x5178;)
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-decorator">@sio.on(&apos;disconnect&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_disconnect</span><span class="hljs-params">(sid)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x540E;&#x88AB;&#x6267;&#x884C;
    :param sid: string sid&#x662F;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x7684;&#x5BA2;&#x6237;&#x7AEF;id
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-comment"># &#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5F62;&#x5F0F;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E8B;&#x4EF6;&#x7684;&#x5B9A;&#x4E49;&#x7531;&#x524D;&#x540E;&#x7AEF;&#x7EA6;&#x5B9A;</span>
<span class="hljs-decorator">@sio.on(&apos;my custom event&apos;)  </span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_custom_event</span><span class="hljs-params">(sid, data)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#x6D88;&#x606F;&#x7684;&#x5904;&#x7406;&#x65B9;&#x6CD5;
    :param sid: string sid&#x662F;&#x53D1;&#x9001;&#x6B64;&#x4E8B;&#x4EF6;&#x6D88;&#x606F;&#x7684;&#x5BA2;&#x6237;&#x7AEF;id
    :param data: data&#x662F;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6D88;&#x606F;&#x6570;&#x636E;
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<h5 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h5>
<ul>
<li>connect &#x4E3A;&#x7279;&#x6B8A;&#x4E8B;&#x4EF6;&#xFF0C;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x540E;&#x81EA;&#x52A8;&#x6267;&#x884C;</li>
<li><p>disconnect &#x4E3A;&#x7279;&#x6B8A;&#x4E8B;&#x4EF6;&#xFF0C;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x540E;&#x81EA;&#x52A8;&#x6267;&#x884C;</p>
</li>
<li><p>connect&#x3001;disconnect&#x4E0E;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x7684;&#x51FD;&#x6570;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x4E0D;&#x540C;</p>
</li>
</ul>
<h4 id="2&#xFF09;&#x53D1;&#x9001;&#x4E8B;&#x4EF6;&#x6D88;&#x606F;">2&#xFF09;&#x53D1;&#x9001;&#x4E8B;&#x4EF6;&#x6D88;&#x606F;</h4>
<ul>
<li><p>&#x7FA4;&#x53D1;</p>
<pre><code class="lang-python">sio.emit(<span class="hljs-string">&apos;my event&apos;</span>, {<span class="hljs-string">&apos;data&apos;</span>: <span class="hljs-string">&apos;foobar&apos;</span>})
</code></pre>
</li>
<li><p>&#x7ED9;&#x6307;&#x5B9A;&#x7528;&#x6237;&#x53D1;&#x9001;</p>
<pre><code class="lang-python">sio.emit(<span class="hljs-string">&apos;my event&apos;</span>, {<span class="hljs-string">&apos;data&apos;</span>: <span class="hljs-string">&apos;foobar&apos;</span>}, room=user_sid)
</code></pre>
</li>
<li><p>&#x7ED9;&#x4E00;&#x7EC4;&#x7528;&#x6237;&#x53D1;&#x9001;</p>
<p>  SocketIO&#x63D0;&#x4F9B;&#x4E86;<strong>&#x623F;&#x95F4;&#xFF08;room&#xFF09;</strong>&#x6765;&#x4E3A;&#x5BA2;&#x6237;&#x7AEF;&#x5206;&#x7EC4;</p>
<ul>
<li><p><code>sio.enter_room(sid, room_name)</code></p>
<p>  &#x5C06;&#x8FDE;&#x63A5;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x6DFB;&#x52A0;&#x5230;&#x4E00;&#x4E2A;room</p>
<pre><code class="lang-python"><span class="hljs-decorator">  @sio.on(&apos;chat&apos;)</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">begin_chat</span><span class="hljs-params">(sid)</span>:</span>
      sio.enter_room(sid, <span class="hljs-string">&apos;chat_users&apos;</span>)
</code></pre>
<p>  &#x6CE8;&#x610F;&#xFF1A;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;socketio&#x4F1A;&#x81EA;&#x52A8;&#x5C06;&#x5BA2;&#x6237;&#x7AEF;&#x6DFB;&#x52A0;&#x5230;&#x4EE5;&#x6B64;&#x5BA2;&#x6237;&#x7AEF;sid&#x4E3A;&#x540D;&#x7684;room&#x4E2D;</p>
</li>
<li><p><code>sio.leave_room(sid, room_name)</code></p>
<p>  &#x5C06;&#x5BA2;&#x6237;&#x7AEF;&#x4ECE;&#x4E00;&#x4E2A;room&#x4E2D;&#x79FB;&#x9664;</p>
<pre><code class="lang-python"><span class="hljs-decorator">  @sio.on(&apos;exit_chat&apos;)</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">exit_chat</span><span class="hljs-params">(sid)</span>:</span>
      sio.leave_room(sid, <span class="hljs-string">&apos;chat_users&apos;</span>)
</code></pre>
</li>
<li><p><code>sio.rooms(sid)</code></p>
<p>  &#x67E5;&#x8BE2;sid&#x5BA2;&#x6237;&#x7AEF;&#x6240;&#x5728;&#x7684;&#x6240;&#x6709;&#x623F;&#x95F4;</p>
<p>&#x7ED9;&#x4E00;&#x7EC4;&#x7528;&#x6237;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7684;&#x793A;&#x4F8B;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@sio.on(&apos;my message&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">message</span><span class="hljs-params">(sid, data)</span>:</span>
  sio.emit(<span class="hljs-string">&apos;my reply&apos;</span>, data, room=<span class="hljs-string">&apos;chat_users&apos;</span>)
</code></pre>
<p>&#x4E5F;&#x53EF;&#x5728;&#x7FA4;&#x7EC4;&#x53D1;&#x6D88;&#x606F;&#x65F6;&#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x5BA2;&#x6237;&#x7AEF;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@sio.on(&apos;my message&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">message</span><span class="hljs-params">(sid, data)</span>:</span>
  sio.emit(<span class="hljs-string">&apos;my reply&apos;</span>, data, room=<span class="hljs-string">&apos;chat_users&apos;</span>, skip_sid=sid)
</code></pre>
</li>
</ul>
</li>
<li><p>&#x4F7F;&#x7528;<code>send</code>&#x53D1;&#x9001;<code>message</code>&#x4E8B;&#x4EF6;&#x6D88;&#x606F;</p>
<p>  &#x5BF9;&#x4E8E;<strong>&apos;message&apos;&#x4E8B;&#x4EF6;</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;send&#x65B9;&#x6CD5;</p>
<pre><code class="lang-python">  sio.send({<span class="hljs-string">&apos;data&apos;</span>: <span class="hljs-string">&apos;foobar&apos;</span>})
  sio.send({<span class="hljs-string">&apos;data&apos;</span>: <span class="hljs-string">&apos;foobar&apos;</span>}, room=user_sid)
</code></pre>
</li>
</ul>
<h2 id="3-python&#x5BA2;&#x6237;&#x7AEF;">3 Python&#x5BA2;&#x6237;&#x7AEF;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> socketio

sio = socketio.Client()

<span class="hljs-decorator">@sio.on(&apos;connect&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_connect</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-decorator">@sio.on(&apos;event&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_event</span><span class="hljs-params">(data)</span>:</span>
    <span class="hljs-keyword">pass</span>

sio.connect(<span class="hljs-string">&apos;http://10.211.55.7:8000&apos;</span>)
sio.wait()
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C09-IM/WebSocket.html" class="navigation navigation-prev " aria-label="Previous page: WebSocket"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C09-IM/Chat.html" class="navigation navigation-next " aria-label="Next page: 头条聊天服务实现"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
